package com.example.xueshen.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xueshen.common.entity.*;
import com.example.xueshen.common.service.BanService;
import com.example.xueshen.common.service.CourseService;
import com.example.xueshen.common.service.TeacherService;
import com.example.xueshen.common.util.MyPageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
public class BanController {
    @Resource
    private BanService banService;
    @Resource
    private CourseService courseService;
    ;
    @GetMapping("/ban")
    public String select(PageData pageData, Map<String, Object> map,String search) {
        Page<Ban> page = new Page<>(pageData.getCurrent(), pageData.getSize());
        if(search==null){
            search = "undefined";
        }if(search.equals("undefined")){
            search = null;
        }
        if (search !=null){
            QueryWrapper<Ban> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("class_name",search);
            Page<Ban>  studentsPage= banService.selectBy(page, queryWrapper);

            for (Ban ban: studentsPage.getRecords()) {
                Course course = courseService.getById(ban.getCourseId());
                ban.setCourse(course);
            }
            if(page.getTotal() != 0) {
                /*这段代码调用了一个名为`getNavigatePageNums`的方法，它位于名为`MyPageUtil`的类中。这个方法接收三个参数：当前页码、总页数和需要显示的导航页码数。

它返回一个`int`类型的数组，包含了需要在页面上显示的导航页码。导航页码是指在页面上显示的一组页码，用户可以通过点击它们来切换到相应的页面。例如，一个包含10页的列表，如果每次只显示3个导航页码，那么就会有3个页码一组，分别显示为1-3、4-6、7-10。

在这个代码片段中，`pageNums`数组接收了`getNavigatePageNums`方法的返回值，在接下来的操作中，它可能会用来在页面上显示一组导航页码。*/
                int[] pageNums = MyPageUtil.getNavigatePageNums((int) studentsPage.getCurrent(), (int) studentsPage.getPages(), 3);
                map.put("pageNums", pageNums);
            }
            map.put("page", studentsPage);
        }else {

            Page<Ban> studentsPage = banService.selectAllByPlusAndPage(page);

            for (Ban ban : studentsPage.getRecords()) {
                Course course = courseService.getById(ban.getCourseId());
                ban.setCourse(course);


            }
            if (page.getTotal() != 0) {
                int[] pageNums = com.example.xueshen.common.util.MyPageUtil.getNavigatePageNums((int) studentsPage.getCurrent(), (int) studentsPage.getPages(), 3);
                map.put("pageNums", pageNums);
            }
            map.put("page", studentsPage);
            if (page.getTotal() != 0) {
                int[] pageNums = MyPageUtil.getNavigatePageNums((int) studentsPage.getCurrent(), (int) studentsPage.getPages(), 3);
                map.put("pageNums", pageNums);
            }
            map.put("page", studentsPage);
        }
        return "student/ban";
    }

    @GetMapping("/bantoadd")
    public String toAdd(Map<String, Object> map) {
//        List<Ban> bans = banService.selectAllByPlus();
        List<Course> courses = courseService.getAllByPlus();
//        map.put("bans", bans);
        map.put("courses", courses);
        return "student/ban :: addOrUpdate";
    }

    @GetMapping("/ban/{classId}")
    public String toUpdate(@PathVariable("classId") Integer banId, Map<String, Object> map) {
        Ban ban = banService.selectOneById(banId);
        List<Course> courses = courseService.getAllByPlus();
        map.put("ban", ban);
        map.put("courses", courses);
        return "student/ban :: addOrUpdate";
    }


    @PostMapping("/add")
    @ResponseBody
    public String add(@Valid Ban ban) {
        Boolean flag = banService.insert(ban);
        return flag ? "添加成功" : "添加失败";
    }
    @PutMapping("/add")
    @ResponseBody
    public String update(@Valid Ban ban) {
        boolean flag = banService.updateById(ban);
        return flag ? "修改成功" : "修改失败";
    }
    @GetMapping("ban/delete/{banId}")
    @ResponseBody
    public String delete(@PathVariable Integer banId, Map<String ,Object> map){

        boolean b = false;
        try {
            b = banService.deleteById(banId);
        } catch (Exception e) {
            return "失败";
        }
        return b?"成功":"失败";
    }
}